package com.chuanjiabao.wisdom.util.dialog;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;

import com.chuanjiabao.wisdom.R;
import com.chuanjiabao.wisdom.bean.HomeListCallBackBean;
import com.chuanjiabao.wisdom.databinding.DialogHomeListBinding;
import com.chuanjiabao.wisdom.listener.OnHomeListClickListener;
import com.chuanjiabao.wisdom.ui.view.SwipeItemLayout;
import com.chuanjiabao.wisdom.util.GsonUtil;
import com.chuanjiabao.wisdom.util.HTTPAPI;
import com.chuanjiabao.wisdom.util.SpResourceUtil;
import com.chuanjiabao.wisdom.util.StringUtil;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.utils.OkLogger;

import net.idik.lib.slimadapter.SlimAdapter;
import net.idik.lib.slimadapter.SlimInjector;
import net.idik.lib.slimadapter.viewinjector.IViewInjector;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Response;

public class HomeListDialog extends HomeBaseDialog implements View.OnClickListener {

    private Context context;
    private String hasHome;
    private DialogHomeListBinding binding;

    private SlimAdapter adapter;
    private List<HomeListCallBackBean.DataBean> homeList = new ArrayList<>();

    private OnHomeListClickListener listener;

    public void setOnHomeListClickListener(OnHomeListClickListener listClickListener) {
        this.listener = listClickListener;
    }

    public HomeListDialog(Context context, String hasHome) {
        super(context);
        this.context = context;
        this.hasHome = hasHome;
        binding = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.dialog_home_list, null, false);
        binding.setDialog(this);
        setContentView(binding.getRoot());
        WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
        layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
        layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
        getWindow().setAttributes(layoutParams);
        getWindow().setGravity(Gravity.TOP);
        initView();
    }

    private void initView() {
        binding.llModuleDelete.setOnClickListener(this);
        getHomeList();
        binding.recycler.setLayoutManager(new LinearLayoutManager(context));
        binding.recycler.addOnItemTouchListener(new SwipeItemLayout.OnSwipeItemTouchListener(context));

        //noinspection Convert2Lambda
        adapter = SlimAdapter.create()
                .register(
                        R.layout.item_home_list,
                        new SlimInjector<HomeListCallBackBean.DataBean>() {
                            @Override
                            public void onInject(@NotNull HomeListCallBackBean.DataBean data, @NotNull IViewInjector injector) {
                                String name = TextUtils.isEmpty(data.getTruename()) ? "" : data.getTruename().substring(0, 1);
                                injector.text(R.id.tvModuleName, name)
                                        .text(R.id.tvModuleName2, data.getTruename() + "的家")
                                        .text(
                                                R.id.tvModulePersionNum,
                                                data.getNum() < 0 ? "" : "人数：" + data.getNum()
                                        )
                                        .with(R.id.llModule, view -> {
                                            if (data.getLast_one() == 1) {
                                                view.setBackgroundResource(R.drawable.module_draw_home_list_bg);
                                            } else {
                                                view.setBackgroundResource(R.drawable.module_draw_home_list_second_bg);
                                            }
                                        })
                                        .with(
                                                R.id.tvModuleMaster,
                                                view -> view.setVisibility(
                                                        TextUtils.equals(data.getAdmin_id() + "", SpResourceUtil.getUserId()) ? View.VISIBLE : View.GONE
                                                )
                                        )
                                        .with(
                                                R.id.tvModuleCreate,
                                                view -> view.setVisibility(data.getAdmin_id() < 0 ? View.VISIBLE : View.GONE)
                                        )
                                        .with(
                                                R.id.tvModuleRelation,
                                                view -> view.setVisibility(
                                                        data.getStatus() == 1
                                                                && !TextUtils.equals(data.getAdmin_id() + "", SpResourceUtil.getUserId())
                                                                ? View.VISIBLE
                                                                : View.GONE
                                                )
                                        )
                                        .clicked(R.id.llModule, v -> {
                                            if (listener != null) {
                                                if (data.getId() < 0) {
                                                    listener.setOnHomeListClickListener("create", "", "");
                                                } else if (data.getStatus() == 1) {
                                                    if (TextUtils.equals(data.getAdmin_id() + "", SpResourceUtil.getUserId())) {
                                                        listener.setOnHomeListClickListener("enter", data.getId() + "", "");
                                                    } else {
                                                        listener.setOnHomeListClickListener("relation", data.getId() + "", "");
                                                    }
                                                } else {
                                                    listener.setOnHomeListClickListener("enter", data.getId() + "", "");
                                                }
                                            }
                                            dismiss();
                                        })
                                        .clicked(R.id.ivModuleDelete, v -> {
                                            listener.setOnHomeListClickListener(
                                                    "delete",
                                                    data.getId() + "",
                                                    data.getAdmin_id() + ""
                                            );
                                            dismiss();
                                        });
                            }
                        })
                .attachTo(binding.recycler);

        adapter.updateData(homeList);
    }

    private void getHomeList() {
        HTTPAPI.getInstance().getHomeList(new StringCallback() {
            @Override
            public void onSuccess(String s, Call call, Response response) {
                if (!StringUtil.isFail(s)) {
                    StringUtil.showFail(context, s);
                } else {
                    HomeListCallBackBean callBackBean = GsonUtil.GsonToBean(s, HomeListCallBackBean.class);
                    List<HomeListCallBackBean.DataBean> items = callBackBean.getData();
                    if (items != null) {
                        homeList.clear();
                        if (TextUtils.equals("0", hasHome)) {
                            HomeListCallBackBean.DataBean data = new HomeListCallBackBean.DataBean();
                            data.setTruename("我自己");
                            data.setId(-1);
                            data.setAdmin_id(-1);
                            data.setCreate_id(-1);
                            data.setNum(-1);
                            data.setLast_one(0);
                            homeList.add(data);
                        }
                        homeList.addAll(items);
                    } else {
                        if (TextUtils.equals("0", hasHome)) {
                            HomeListCallBackBean.DataBean data = new HomeListCallBackBean.DataBean();
                            data.setTruename("我自己");
                            data.setId(-1);
                            data.setAdmin_id(-1);
                            data.setCreate_id(-1);
                            data.setNum(-1);
                            data.setLast_one(0);
                            homeList.add(data);
                        }
                    }

                    HomeListCallBackBean.DataBean myData = null;
                    for (HomeListCallBackBean.DataBean item : items) {
                        if (TextUtils.equals(item.getCreate_id() + "", SpResourceUtil.getUserId())) {
                            myData = item;
                        }
                    }

                    if (myData != null) {
                        homeList.remove(myData);
                        myData.setTruename("我自己");
                        homeList.add(0, myData);
                    }
                    adapter.notifyDataSetChanged();
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.llModuleDelete) {
            dismiss();
        }
    }
}


